
import commands
import subprocess
import sys
import time
import paramiko

commandMac = 'ssh %s@%s "%s"'
commandWindows = 'ssh %s@%s "%s"'

commandMap = {'darwin':commandMac, 'win32':commandWindows}

class SSH:
	def __init__(self, hostname, username, password, command):
		self.hostname = hostname
		self.username = username
		self.password = password
		self.command = command
		self.output = None
		self.error = None
		self.client = None
	
	def getOutput(self):
		if self.output is None:
			self.run()
		return self.output
	
	def getError(self):
		if self.error is None:
			self.run()
		return self.error
	
	def run(self):
		if self.client is None:
			client = paramiko.SSHClient()
			client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
			client.connect(self.hostname, username=self.username, password=self.password)
			self.client = client
		stdin, stdout, stderr = self.client.exec_command(self.command)
		stdin.close()
		self.output = ''.join(stdout.readlines())
		self.error = ''.join(stderr.readlines())

def main():
	client = SSH('daks.ucdavis.edu', username='sean', password='Phexican', command='ls')
	print 'o:', client.getOutput()
	print 'e:', client.getError()
	print 'All done!'

if __name__ == '__main__':
		main()
